<template>
  <div class="container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="card-header clearfix">
            <h5 class="title pull-left">科室覆盖</h5>
          </div>
          <departments-chart
            :chart-data="departmentsStatistics"
            :height="'800px'"
          />
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt20">
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="card-header clearfix">
            <h5 class="title pull-left">省份分布</h5>
          </div>
          <el-row :gutter="20">
            <el-col :xs="24" :sm="24" :lg="12">
              <province-map
                :chart-data="provinceStatistics"
                :height="'800px'"
              />
            </el-col>
            <el-col :xs="24" :sm="24" :lg="12">
              <province-chart
                :chart-data="provinceData"
                :height="'800px'"
              />
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getDistributionStatistics,
  exportDistributionStatistics
} from '@/api/statistics'
import DepartmentsChart from './components/DepartmentsChart'
import ProvinceChart from './components/ProvinceChart'
import ProvinceMap from './components/ProvinceMap'

export default {
  name: 'DistributionStatistics',
  components: {
    DepartmentsChart,
    ProvinceChart,
    ProvinceMap
  },
  data() {
    return {
      departmentsStatistics: [],
      provinceStatistics: [],
      provinceData: {
        name: [],
        value: []
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getDistributionStatistics().then(response => {
        // console.log(response)
        const { data } = response
        data.deparrment.forEach(item => {
          if (item.count !== 0) {
            this.departmentsStatistics.push({
              name: item.name,
              value: item.count
            })
          }
        })
        data.province.forEach(item => {
          if (item.count !== 0) {
            this.provinceStatistics.push({
              name: item.name,
              value: item.count
            })
          }

          this.provinceData.name.push(this._.isEmpty(item.name) ? '未知地区' : item.name)
          this.provinceData.value.push(item.count)
        })
      }).catch(err => {
        console.log(err)
      })
    },
    downloadChart() {
      exportDistributionStatistics().then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '导出成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: '导出失败，请稍后再试'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 40px;
}
</style>
